import { MARKET_SUFFIX_NAME } from "utils/timeData";
// const marketCategory = require(".market").market();
import marketCategory from "./market";
/**
 * 获取 当前股票的简单信息
 * @param {String} symbol 代码
 */
export const getSymbolSimpleDetail = (symbol) => {
  // 获取股票所在的市场代码
  let marketCodes = [];
  marketCategory.map((item) => {
    item.marketCodes.map((res) => {
      if (res.indexOf(symbol.split(".")[1]) > -1) {
        marketCodes.push(res);
      }
    });
  });
  if (marketCodes.length) {
    // 获取市场下的所有代码的简单信息
    const marketDetailList = [
        ...ucf.api.fin.cache.getProductList(marketCodes),
        ...indexStock,
      ],
      detail = marketDetailList.find((res) => {
        return res.prod_code === symbol.split(".")[0];
      });
    if (detail) {
      return detail;
    }
    return {};
  }
  return {};
};

/**
 * @description 统一修改外汇名称
 * @param {String} prod_code 代码code
 */
export const formatProdName = (stockCodeKey) => {
  const isGlobalForeign = MARKET_SUFFIX_NAME.IsGlobalForeignCurrency({
    finance_mic: stockCodeKey.finance_mic,
    hq_type_code: stockCodeKey.hq_type_code,
  });
  if (isGlobalForeign) {
    return stockCodeKey.prod_name.replace("即期报价", "");
  }
  return stockCodeKey.prod_name;
};
/**
 * @description 统一修改外汇名称
 * @param {String} prod_code 代码code
 */
export const formatProdCode = (stockCodeKey) => {
  if (stockCodeKey.hq_type_code) {
    //  外汇或债券 格式化代码字段
    const isForeign = MARKET_SUFFIX_NAME.IsForeignCurrency(null, {
      finance_mic: stockCodeKey.finance_mic,
      hq_type_code: stockCodeKey.hq_type_code,
    });
    const interbank_bond_market = MARKET_SUFFIX_NAME.IsInterbankBond({
      finance_mic: stockCodeKey.finance_mic,
      hq_type_code: stockCodeKey.hq_type_code,
    });
    const interbankBondForward = MARKET_SUFFIX_NAME.IsInterbankBondForward({
      finance_mic: stockCodeKey.finance_mic,
      hq_type_code: stockCodeKey.hq_type_code,
    });
    const IsGlobalForeign = MARKET_SUFFIX_NAME.IsGlobalForeignCurrency({
      finance_mic: stockCodeKey.finance_mic,
      hq_type_code: stockCodeKey.hq_type_code,
    });
    if (interbank_bond_market || interbankBondForward) {
      let prod_code = stockCodeKey.prod_code;
      // 去除CN开头的 CN
      if (prod_code.startsWith("CN")) {
        prod_code = prod_code.replace("CN", "");
      }
      return prod_code.replace(".T", "").replace(".Q", "");
    }
    if (IsGlobalForeign) {
      return stockCodeKey.prod_code.replace(".XHCJ", "");
    }
    if (isForeign) {
      let prod_code = stockCodeKey.prod_code;
      // 去除CN开头的 CN
      // if (prod_code.startsWith('CN')) {
      //   prod_code = prod_code.replace("CN", "")
      // }
      return prod_code
        .replace(".FX", "")
        .replace(".T", "")
        .replace(".Q", "")
        .replace(".CPR", "")
        .replace(".I", "");
    }
  } else {
    const isBond = MARKET_SUFFIX_NAME.IsBond({
      finance_mic: stockCodeKey.finance_mic,
      hq_type_code: stockCodeKey.hq_type_code,
    });
    const isForeign = MARKET_SUFFIX_NAME.IsForeignCurrency(null, {
      finance_mic: stockCodeKey.finance_mic,
      hq_type_code: stockCodeKey.hq_type_code,
    });

    if (isForeign) {
      let prod_code = stockCodeKey.prod_code;
      return prod_code
        .replace(".FX", "")
        .replace(".T", "")
        .replace(".Q", "")
        .replace(".CPR", "")
        .replace(".I", "");
    }
    if (isBond) {
      let prod_code = stockCodeKey.prod_code;
      // 去除CN开头的 CN
      if (prod_code.startsWith("CN")) {
        prod_code = prod_code.replace("CN", "");
      }
      return prod_code.replace(".T", "");
    }
  }

  return stockCodeKey.prod_code;
};

/**
 * @description 根据marketCode获取市场名称
 * @param {String} marketCode
 */
export const getMarketNameByCode = (marketCode) => {
  let marketName = "沪A";
  if ("XSHG" === marketCode || marketCode.startsWith("XSHG.ESA")) {
    marketName = "沪A";
  } else if (marketCode.startsWith("XSHG.ESB")) {
    marketName = "沪B";
  } else if ("XSHE" === marketCode || marketCode.startsWith("XSHE.ESA")) {
    marketName = "深A";
  } else if (marketCode.startsWith("XSHE.ESA")) {
    marketName = "深B";
  } else if (marketCode.startsWith("XSHG.KSH")) {
    marketName = "科创板";
  } else if (marketCode.startsWith("NEEQ")) {
    marketName = "新三板";
  } else if (
    marketCode.startsWith("XSHO") ||
    marketCode.startsWith("XSGE-O") ||
    marketCode.startsWith("XDCE-O") ||
    marketCode.startsWith("XZCE-O")
  ) {
    marketName = "期权";
  } else if (
    marketCode.startsWith("CCFX") ||
    marketCode.startsWith("XSGE") ||
    marketCode.startsWith("XDCE") ||
    marketCode.startsWith("XZCE") ||
    marketCode.startsWith("XINE")
  ) {
    marketName = "期货";
  } else if (
    marketCode.startsWith("XHKG-M") ||
    marketCode.startsWith("XHKG-G") ||
    marketCode.startsWith("XHKG-I") ||
    marketCode.startsWith("XBHK")
  ) {
    marketName = "港股";
  } else if (marketCode.startsWith("XHKG-SZ")) {
    marketName = "深港通";
  } else if (
    marketCode.startsWith("XNYS") ||
    marketCode.startsWith("XASE") ||
    marketCode.startsWith("XNAS") ||
    marketCode.startsWith("XBUS.ZG") ||
    marketCode.startsWith("XBUS.MX")
  ) {
    marketName = "美股";
  } else if (marketCode.match(/^XBHS([\\s\\S]*?)|([\\s\\S]*?).MRI$/)) {
    marketName = "指数";
  } else if (marketCode.match(/.D$/)) {
    marketName = "债券";
  } else if (
    marketCode.match(/.EU$/) ||
    marketCode.startsWith("XSHG.EM") ||
    marketCode.startsWith("XSHE.EM")
  ) {
    marketName = "基金";
  } else if (marketCode.startsWith("FXCFES")) {
    marketName = "外汇";
  }
  return marketName;
};
/**
 * @description 新码表根据secucategorycodeii获取市场名称
 * @param {String} codeii
 * @param {String} market
 */
export const getMarketName = (codeii, market) => {
  let marketNameDic = new Map();
  marketNameDic.set("101XSHG", "沪A主板");
  marketNameDic.set("101XSHE", "深A主板");
  marketNameDic.set("102XSHG", "上海B股");
  marketNameDic.set("102XSHE", "深圳B股");
  marketNameDic.set("204XSHG", "上海基金");
  marketNameDic.set("204XSHE", "深圳基金");
  marketNameDic.set("206XSHG", "上海基金");
  marketNameDic.set("206XSHE", "深圳基金");
  marketNameDic.set("103", "股转系统");
  marketNameDic.set("104", "优先股");
  marketNameDic.set("105", "港股");
  marketNameDic.set("106", "创业板");
  marketNameDic.set("107", "科创板");
  marketNameDic.set("108", "CDR");
  marketNameDic.set("208", "香港基金");
  marketNameDic.set("301", "股票指数");
  marketNameDic.set("302", "基金指数");
  marketNameDic.set("303", "债券指数");
  marketNameDic.set("304", "理财指数");
  marketNameDic.set("305", "期货指数");
  marketNameDic.set("306", "汇率指数");
  marketNameDic.set("307", "组合指数");
  marketNameDic.set("309", "利率指数");
  marketNameDic.set("501", "国债");
  marketNameDic.set("502", "地方债");
  marketNameDic.set("503", "央行票据");
  marketNameDic.set("504", "同业存单");
  marketNameDic.set("505", "金融债");
  marketNameDic.set("506", "金融债");
  marketNameDic.set("507", "企业债");
  marketNameDic.set("508", "公司债");
  marketNameDic.set("512", "中期票据");
  marketNameDic.set("513", "短融");
  marketNameDic.set("514", "超短融");
  marketNameDic.set("515", "次级债");
  marketNameDic.set("516", "可转债");
  marketNameDic.set("522", "回购");
  marketNameDic.set("523", "拆借");
  marketNameDic.set("524", "大额存单");
  marketNameDic.set("525", "利率互换");
  marketNameDic.set("526", "借券借贷");
  marketNameDic.set("527", "远期利率");
  marketNameDic.set("610", "商品期货");
  marketNameDic.set("620", "货币期货");
  marketNameDic.set("630", "利率期货");
  marketNameDic.set("640", "股指期货");
  marketNameDic.set("701", "股票期权");
  marketNameDic.set("702", "ETF期权");
  marketNameDic.set("703", "商品期权");
  marketNameDic.set("704", "股指期权");
  marketNameDic.set("705", "外汇期权");
  marketNameDic.set("801", "外汇即期");
  marketNameDic.set("802", "外汇远期");
  marketNameDic.set("803", "外汇掉期");
  marketNameDic.set("905", "香港权证");
  marketNameDic.set("1*", "股票");
  marketNameDic.set("2*OF", "场外基金");
  marketNameDic.set("2*", "基金");
  marketNameDic.set("3*", "指数");
  marketNameDic.set("4*", "现货");
  marketNameDic.set("5*", "债券");
  marketNameDic.set("6*", "期货");
  marketNameDic.set("7*", "期权");
  marketNameDic.set("8*", "外汇");
  marketNameDic.set("9*", "权证");
  let codeiiMarket = codeii + market,
    codeii0Market = `${String(codeii)[0]}*${market}`,
    codeii0 = `${String(codeii)[0]}*`;
  if (marketNameDic.get(codeiiMarket)) {
    return marketNameDic.get(codeiiMarket);
  } else if (marketNameDic.get(String(codeii))) {
    return marketNameDic.get(String(codeii));
  } else if (marketNameDic.get(codeii0Market)) {
    return marketNameDic.get(codeii0Market);
  } else {
    return marketNameDic.get(codeii0) ? marketNameDic.get(codeii0) : "其他";
  }
};
